from hashlib import md5

from constant.BlogConstant import BlogConstant
from entity.models import db, User


def verify_login(user, user_type="web"):
    # query user base user.acctount
    if user_type == BlogConstant.user_type_web:
        query_user = db.session.query(User).filter(User.account == user['username']).filter(
            User.type == user_type).first()
        # password1 =user.slat+user.password
        if query_user is not None:
            user_input_password1 = user['password'] + query_user.salt
        # md5 encode password1 = password
            user_input_password1 = str(md5(user_input_password1.encode('utf8')).hexdigest())
            if user_input_password1 != query_user.password:
                return False
            del user["password"]
            user["email"] = query_user.email
            user["avatar"] = query_user.avatar
            user["nick_name"] = query_user.nick_name
            user["is_admin"] = query_user.is_admin
            user["uuid"] = query_user.uuid
            return True
        else:
            return False
    else:
        return False
